Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pthreads support for Node.js #9745

Merged
merged 32 commits into from
Nov 1, 2019
Merged

Pthreads support for Node.js #9745

merged 32 commits into from
Nov 1, 2019

Conversation

kripken
Copy link
Member

@kripken kripken commented Oct 31, 2019

This ended up tricky (see the dates on the commits...) because of various node/web differences, like the global scope, the event loop, APIs, etc. With help on #6567 I think this PR finally manages to make it work, at least for an initial "hello world" type test. Thanks to everyone on that issue and in particular @addaleax!

Followups should add a lot more tests. Sadly I don't think we can just reuse the existing browser tests, as they make some web assumptions (like reporting the result using an xhr, when it's ok to block and when not, etc.).

This moves some code from shell.js into side .js files, so that we can use it in multiple places.

Fixes #6567

Copy link

@addaleax addaleax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/worker.js Outdated Show resolved Hide resolved
var nodeFS = require('fs');

var nodeRead = function(filename) {
return nodeFS.readFileSync(filename).toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nodeFS.readFileSync(filename).toString();
return nodeFS.readFileSync(filename, 'utf8');

(Just a minor thing, it avoids creating an intermediate Buffer/copying it into a string)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! I think it's best to open a separate PR for that later as I see we have more than one existing call like that that could be improved that way.

src/worker.js Show resolved Hide resolved
src/library_pthread.js Outdated Show resolved Hide resolved
@kripken kripken requested a review from sbc100 October 31, 2019 22:41
@kripken kripken requested a review from dschuff October 31, 2019 22:41
src/compiler.js Outdated Show resolved Hide resolved
src/runtime_init_memory.js Show resolved Hide resolved
src/shell.js Show resolved Hide resolved
src/shell.js Show resolved Hide resolved
src/shell.js Outdated Show resolved Hide resolved
@@ -0,0 +1,62 @@
// Copyright 2019 The Emscripten Authors. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have tests/pthread/test_pthread_create.cpp already. How does tests/core/pthread relate to the tests in tests/pthread?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/pthread/ are existing tests, that only work on the web. The new core/pthreads are for the core test suite and run on node.

Sadly the web tests can't just be reused as they make some web assumptions (like reporting the result using an xhr, when it's ok to block and when not, etc., which it turns out node differs on).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. We should probably document somewhere (even if it's just for our own testing purposes) exactly the differences between web-pthreads and node-pthreads environments, and it would be nice to eventually unify the tests. But that doesn't need to be in this PR.

CreateThread(i);
}

// Node.js requires the event loop to be reached for the worker to start up.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that PTHREAD_POOL doesn't work for node?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it should. It didn't in earlier work here, but looks like the current PR got it to work! I'm adding a test for the pool allowing works to be synchronously available.

@abram
Copy link

abram commented Jan 31, 2020

Hi @kripken, thanks for your work on this! I'm running into an issue and I wanted to check in with you before working on a fix.

I'm currently deploying a multithreaded application using Emscripten inside Electron, and this change appears to have broken things for me - I get errors like worker.on is not a function. I think the problem stems from the Electron issue electron/electron#18540. Inside current Electron windows/workers (when the properties nodeIntegration/nodeIntegrationInWorker are set to true), most Node APIs are available, but the worker_threads API isn't. Since Emscripten attempts to use the Node API instead of the web one, setup fails.

The simplest solution I can think of is to switch the priorities, so web workers are preferred over node workers. Or additional checks could be added to confirm that the Node worker_threads API is available before choosing to use it.

What do you think? If it would be helpful I could put together a minimal test app demonstrating the problem.

@kripken
Copy link
Member Author

kripken commented Jan 31, 2020

@abram Interesting, I didn't realize how complex this could be I guess 😄 Are you saying we might check, in ENVIRONMENT_HAS_NODE whether we have Web (non-Node) Workers and use them if so? That sounds good to me if it works, PR would be welcome!

@abram
Copy link

abram commented Feb 1, 2020

@kripken Yes, that sounds right. I'll work on a PR. Thanks!

belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
This ended up tricky because of various node/web differences, like the global
scope, the event loop, APIs, etc. With help on emscripten-core#6567 I think this PR finally
manages to make it work, at least for an initial "hello world" type test.
Thanks to everyone on that issue and in particular @addaleax!

This moves some code from shell.js into side .js files, so that we can use it in
multiple places.

Fixes emscripten-core#6567 but as we add more tests I'm sure we'll find more issues, this just
shows basic functionality so far. We may not be able to easily reuse the
existing browser tests as they have browser-specific things in them, but we
can try.
belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
Copy link

@nohhhon nohhhon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node -v
npm -v

@@ -432,6 +432,18 @@ var LibraryPThread = {
worker.onerror = function(e) {
err('pthread sent an error! ' + e.filename + ':' + e.lineno + ': ' + e.message);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storage Allocation Method. Hardware is needed.. static Ram. And bios need to be on an external harddrive. .3 disk and 5 readers . With the modifications of each disc except for the center disc need to rotate at a syncrinive rpm in opposite directions this the nodes and threads will be will be twin cousins to the interface OS.. thus the external hardware will see them as one until data is deleted... The electron nodes only take in and recognize hardware attributed firmware ,threads don't recognize any thing except nodes. Environment is semi magnetic before and virtual after... Thus nodes and threads need to be external of an O.S,. [π¥] ..Z +Z²=©°![Uploading b5{3c0afaa} (ae8ba507-5d6d-488c-88eb-8b4ffa783c47 id=0x7f0a0v.www.darpa.mil

ret = tryParseAsDataURI(filename);
if (!ret) {
#endif
if (!nodeFS) nodeFS = require('fs');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/node_shell_read.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants